<template>
    <div>
        <el-card class="operate-container" shadow="never">
            <div style="display: flex;height: 30px;">
                <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
                    <el-form-item label="所属城市：">
                        <el-select v-model="listQuery.id" clearable
                            filterable
                            remote
                            :remote-method="queryCityd"
                            placeholder="请搜索"  >
                            <el-option
                                v-for="item in fourthOptions"
                                :key="item.id"
                                :label="item.cn ? item.cn + ' | ' + item.zhHant + ' | ' + item.country.cn : item.name + ' | ' + item.country.cn"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <el-button
                    style="float:right"
                    type="primary"
                    @click="handleSearchList()"
                    size="small">
                    查询搜索
                </el-button>
                <el-button
                    style="float:right;margin-right: 15px"
                    @click="handleResetSearch()"
                    size="small">
                    重置
                </el-button>
            </div>
            <span class="btn-list" @click="refBtn(4)">刷新列表</span>
        </el-card>
        <div class="table-container">
            <el-table ref="fourthTable"
                        :data="targetList"
                        row-key="id"
                        v-loading="listLoading"
                        style="width: 100%;"
                            border>
                <el-table-column label="城市ID" align="center">
                    <template slot-scope="scope">{{scope.row.id}}</template>
                </el-table-column>
                <el-table-column label="城市名称（原名）" align="center">
                    <template slot-scope="scope">{{scope.row.name }} </template>
                </el-table-column>
                <el-table-column label="城市名称（简体|繁体）" align="center">
                    <template slot-scope="scope">{{scope.row.cn}} | {{scope.row.zhHant}}</template>
                </el-table-column>
                <el-table-column label="操作" width="360" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini"
                                type="text"
                                @click="fourthEdite(scope.$index, scope.row)">编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :current-page.sync="listQuery.pageNum"
            :page-size="listQuery.pageSize"
            :page-sizes="[10,15,20]"
            :total="total">
            </el-pagination>
        </div>

        <el-dialog
            :title="'编辑'"
            :visible.sync="fourthVisible"
            width="55%">
            <el-form :model="fourthResource"
                    ref="fourthForm"
                    :rules="rules"
                    label-width="150px" size="small">
                <el-form-item label="城市（原名）：" prop="name">
                    <el-input :disabled="isEdit" v-model="fourthResource.name" placeholder="输入分组名称，该名称不可修改" style="width: 250px" maxlength="30"></el-input>
                </el-form-item>
                <el-form-item label="城市（简体中文）：" prop="cn">
                    <el-input v-model="fourthResource.cn" style="width: 250px" maxlength="30"></el-input>
                </el-form-item>
                <el-form-item label="城市（繁体中文）：" prop="zhHant">
                    <el-input v-model="fourthResource.zhHant" style="width: 250px" maxlength="30"></el-input>
                </el-form-item>
                <el-form-item label="城市（拼音）：" prop="py">
                    <el-input v-model="fourthResource.py" style="width: 250px" maxlength="30"></el-input>
                </el-form-item>
                <el-form-item label="城市（拼音首字母）：" prop="py1">
                    <el-input v-model="fourthResource.py1" style="width: 250px" maxlength="30"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="fourthVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="fourthDialogConfirm('fourthForm')" size="small">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {  cityList,  cityPage,
        destinationDelete, destinationAdd, cityPageUpdate, countryQuer } from '@/api/app';
const defaultListQuery = {
    pageNum: 0,
    pageSize: 15,
    id: null,
};
export default {
    name: 'city',
    data() {
        return {
            rules: {
                cn: [
                    { required: true, message: '请填写简体中文名' }
                ],
                zhHant: [
                    { required: true, message: '请填写繁体中文名' }
                ],
                py: [
                    { required: true, message: '请填写拼音' }
                ],
                py1: [
                    { required: true, message: '请填写拼音首字母' }
                ],
            },
            isCountry: false,
            listQuery: Object.assign({}, defaultListQuery),
            isOneS: false,
            targetList: [],
            thirdOptions: [],
            listLei: [],
            fourthVisible: false,
            fourthResource: {
                _id: null,
                cityId: null,
                countryCode: null,
                name: null,
                cn: null,
                py: null,
                py1: null,
                zhHant: null,
            },
            fourthOptions: [],
            isEdit: false,
            sortBoList: [],
            cityFirst: null,
            listLoading: false,
            sortShow: null,
            total: 0,
        }
    },
    mounted() {

    },
    watch: {

    },
    created() {
        this.fourthList();
    },
    methods: {
        queryCountry(key) {
            if (key) {
                countryQuer({keyword: key}).then(res => {
                    this.thirdOptions = res.data;
                });
            }
        },
        queryCityd(key) {
            if (key) {
                cityList({keyword: key}).then(res => {
                    this.fourthOptions = res.data;
                });
            }
        },
        fourthList() {
            this.listLoading = true;
            cityPage(this.listQuery).then(res => {
                setTimeout(() => {
                    this.listLoading = false;
                }, 300)
                this.targetList = res.data.content;
                this.total = res.data.totalElements;
            });
        },
        refBtn(type) {
            this.fourthList();
        },
        handleResetSearch() {
            this.isCountry = false;
            this.listQuery = Object.assign({}, defaultListQuery);
            this.fourthList();
        },
        handleSearchList() {
            if (this.listQuery.id) {
                this.isCountry = true;
            } else {
                this.isCountry = false;
            }
            this.fourthList();
        },
        fourthAdd() {
            this.fourthResource = {
                id: null,
                countryEsId: null,
                cityEsId: null,
                status: 1,
                sort: null,
                updateTime: null,
                createTime: null,
                deleteYn: null,
            };
            this.isEdit = false;
            this.fourthVisible = true;
        },
        fourthDialogConfirm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isEdit) {
                        cityPageUpdate(this.fourthResource).then(res => {
                            this.$message({
                                type: 'success',
                                message: '修改成功!'
                            });
                            this.fourthVisible = false;
                            this.fourthList();
                        });
                    }
                } else {
                    return false;
                }
            });
        },
        fourthEdite(index, row) {
            this.fourthResource = row;
            this.isEdit = true;
            this.fourthVisible = true;
        },
        fourthDelete(index, row) {
            this.$confirm('删除后该目的地会被清除，你还要继续吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                destinationDelete(row.id).then(response => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.fourthList();
                });
            });
        },
        handleSizeChange(val) {
          this.listQuery.pageNum = 0;
          this.listQuery.pageSize = val;
          this.fourthList();
        },
        handleCurrentChange(val) {
          this.listQuery.pageNum = val;
          this.fourthList();
        },
    }
}
</script>

<style lang="scss">
.btn-list {
    cursor: pointer;
}
</style>
